package com.google.precinct.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.precinct.client.finder.FindRootPage;
import com.google.precinct.client.mapper.MapRootPage;
import com.google.precinct.common.CityItem;
import com.google.precinct.common.PrecinctInfo;

public class MainShell extends Composite {

	private static MainShellUiBinder uiBinder = GWT
			.create(MainShellUiBinder.class);

	interface MainShellUiBinder extends UiBinder<Widget, MainShell> {
	}

	@UiField
	FlowPanel breadCrumbs;

	@UiField
	SimplePanel content;

	private BreadCrumbManager breadCrumb = null;

	public MainShell() {
		initWidget(uiBinder.createAndBindUi(this));
		this.setWidth("100%");
		breadCrumb = new BreadCrumbManager(breadCrumbs);
		breadCrumb.pushEntry("Home",
				new Runnable() {
			@Override
			public void run() {
				resetToHomePage();
			}
		},
				new Runnable() {
			@Override
			public void run() {
				throw new RuntimeException("This should not happen.");
			}
		});
		resetToHomePage();
	}

	private void swapPanel(Widget page) {
		if (content.getWidget() != null) {
			content.remove(content.getWidget());
		}
		content.add(page);
	}

	public void precinctSearch(CityItem selection, String text) {
		Util.track("/PrecinctFinder/01-PrecinctNoSelect");
//		this.remove(0);
//		this.add(new PrecinctMapPage(this, selection, text));
	}

	public void resetToHomePage() {
		swapPanel(new LandingPage(this));
	}


	public void areaSearch(CityItem selection) {
		Util.track("/PrecinctFinder/01-AreaSelect");
//		this.remove(0);
//		this.add(new PrecinctMapPage(this, selection, null));
	}

	public void mapPrecinct() {
		swapPanel(new MapRootPage(this, breadCrumb));
	}

	public void findPrecinct(PrecinctInfo precinctInfo) {
		swapPanel(new FindRootPage(this, precinctInfo, breadCrumb));
	}



}
